package com.hqyj.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.GoodsInfo;
import com.hqyj.dao.GoodsInfoMapper;
import com.hqyj.pojo.GoodsQueryParameter;
import com.hqyj.pojo.R;
import com.hqyj.service.GoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2023-02-27 10:43:26
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {

    @Resource
    GoodsInfoMapper goodsInfoMapper;
    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public R queryGoodsByFormId(Integer formId) {
        //定义缓存键名
        String key ="goods:index:"+formId;
        //判断缓存是否有数据
        Object obj = redisTemplate.opsForList().range(key,0,-1);
        List<GoodsInfo> list =null;
        if(obj==null){
            //查询数据库的数据
             list = goodsInfoMapper.queryGoodsByFormId(formId);
            //存入缓存
            redisTemplate.opsForList().leftPushAll(key,list);
            //设置缓存时长,缓存时长一般15到1小时
            redisTemplate.opsForList().getOperations().expire(key,15, TimeUnit.MINUTES);
        }else{
            //获取缓存数据
            list = (List<GoodsInfo>) obj;
        }
        return R.ok("success",200,list);
    }

    @Override
    public R queryGoodsThree(Integer formId, Integer goodsTypeId) {
        //定义缓存键名
        String key ="goods:three:"+formId+":"+goodsTypeId;
        //判断缓存是否有数据
        Object obj = redisTemplate.opsForList().range(key,0,-1);
        List<GoodsInfo> list =null;
        if(obj==null){
            //查询数据库的数据
            list = goodsInfoMapper.queryGoodsThree(formId,goodsTypeId);
            //存入缓存
            redisTemplate.opsForList().leftPushAll(key,list);
            //设置缓存时长,缓存时长一般15到1小时
            redisTemplate.opsForList().getOperations().expire(key,15, TimeUnit.MINUTES);
        }else{
            //获取缓存数据
            list = (List<GoodsInfo>) obj;
        }
        return R.ok("success",200,list);
    }

    @Override
    public R queryGoodsByParameter(GoodsQueryParameter g) {
         //定义查询缓存键
        String key ="goods:detail:"
                    +g.getGoodTypeId()+":"
                    +g.getBrandName()+":"
                    +g.getStartPrice()+":"
                    +g.getEndPrice()+":"
                    +g.getColName()+":"
                    +g.getOrderType()+":"
                    +g.getPage()+":"
                    +g.getSize()+":";
        //判断缓存是否有数据
        Object obj = redisTemplate.opsForHash().get(key,"list");
        //创建map对象
        HashMap<String,Object> map = new HashMap<>();
        if(obj==null){
            //1 创建分对象
            Page<GoodsInfo> page = new Page<>(g.getPage(),g.getSize());
            //2 查询数据库分页
            Page<GoodsInfo> infoPage = goodsInfoMapper.queryGoodsByParameter(page,g);

            //记录当前页集合
            map.put("list",infoPage.getRecords());
            //总页数
            map.put("totalPage",infoPage.getPages());
            //总条数
            map.put("total",infoPage.getTotal());
            //存入redis缓存
            redisTemplate.opsForHash().put(key,"list",infoPage.getRecords());
            redisTemplate.opsForHash().put(key,"totalPage",infoPage.getPages());
            redisTemplate.opsForHash().put(key,"total",infoPage.getTotal());
            //设置缓存的时长
            redisTemplate.opsForHash().getOperations().expire(key,15,TimeUnit.MINUTES);
        }else{
            //从缓存读取数据
            List<GoodsInfo> list = (List<GoodsInfo>)redisTemplate.opsForHash().get(key,"list");
            Integer totalPage = (Integer)redisTemplate.opsForHash().get(key,"totalPage");
            Integer total = (Integer)redisTemplate.opsForHash().get(key,"total");
            //记录当前页集合
            map.put("list",list);
            //总页数
            map.put("totalPage",totalPage);
            //总条数
            map.put("total",total);
        }

        return R.ok("success",200,map);
    }
}
